package com.mpc.manager;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mpc.params.MainTabPageParams;
import com.mpc.utils.Log;

public class PageManager {
    public final String TAG = PageManager.class.getSimpleName();

    public static final String CURR_PAGE = "curr_page";

    public static final int INDEX_PAGE_NOT_FOUND = 0;
    public static final int INDEX_PAGE_HOME = 1;
    public static final int INDEX_PAGE_APPS = 2;
    public static final int INDEX_PAGE_CONTACT = 3;

    public static final int INDEX_PAGE_START = INDEX_PAGE_HOME;
    public static final int INDEX_PAGE_END = INDEX_PAGE_CONTACT;

    public static final String NAME_PAGE_HOME = "home";
    public static final String NAME_PAGE_APPS = "apps";
    public static final String NAME_PAGE_CONACT = "contact";

    public static final String PAGE_DIR = "/page";

    public static final String JSP_PATH_PAGE_HOME = String.format("%s/%s.jsp",
            PAGE_DIR, NAME_PAGE_HOME);
    public static final String JSP_PATH_PAGE_APPS = String.format("%s/%s.jsp",
            PAGE_DIR, NAME_PAGE_APPS);
    public static final String JSP_PATH_PAGE_CONTACT = String.format(
            "%s/%s.jsp", PAGE_DIR, NAME_PAGE_CONACT);

    private static PageManager sPageManager = null;

    private final Map<Integer, String> mUriMap = new HashMap<Integer, String>();

    private PageManager() {
        mUriMap.put(INDEX_PAGE_HOME, JSP_PATH_PAGE_HOME);
        mUriMap.put(INDEX_PAGE_APPS, JSP_PATH_PAGE_APPS);
        mUriMap.put(INDEX_PAGE_CONTACT, JSP_PATH_PAGE_CONTACT);
    }

    public static PageManager getInstance() {
        if (sPageManager == null) {
            sPageManager = new PageManager();
        }
        return sPageManager;
    }

    public String getJspPathFromPageCode(int code) {
        return mUriMap.get(code);
    }

    // /mpchome/home_en_xxx.htm
    public int getPageCodeFromHtmlUri(HttpServletRequest request) {
        String uri = request.getRequestURI();
        if (uri == null || uri.length() == 0) {
            return -1;
        }

        if (uri.startsWith(request.getServletContext().getContextPath() + "/"
                + NAME_PAGE_HOME)) {
            return INDEX_PAGE_HOME;
        } else if (uri.startsWith(request.getServletContext().getContextPath()
                + "/" + NAME_PAGE_APPS)) {
            return INDEX_PAGE_APPS;
        } else if (uri.startsWith(request.getServletContext().getContextPath()
                + "/" + NAME_PAGE_CONACT)) {
            return INDEX_PAGE_CONTACT;
        } else {
            return -1;
        }
    }

    // /mpchome/page/home.jsp?lan=en
    public int getPageCodeFromJspUri(HttpServletRequest request) {
        String uri = request.getRequestURI();
        if (uri == null || uri.length() == 0) {
            return -1;
        }

        if (uri.contains(JSP_PATH_PAGE_HOME)) {
            return INDEX_PAGE_HOME;
        } else if (uri.contains(JSP_PATH_PAGE_APPS)) {
            return INDEX_PAGE_APPS;
        } else if (uri.contains(JSP_PATH_PAGE_CONTACT)) {
            return INDEX_PAGE_CONTACT;
        } else {
            return -1;
        }
    }

    // /mpchome/page/home.jsp?lan=en
    public HtmlUri getHtmlPageUriFromJspPageUri(HttpServletRequest request) {
        String uri = request.getRequestURI();
        String base = null;

        if (uri.contains(JSP_PATH_PAGE_HOME)) {
            base = NAME_PAGE_HOME;
        } else if (uri.contains(JSP_PATH_PAGE_APPS)) {
            base = NAME_PAGE_APPS;
        } else if (uri.contains(JSP_PATH_PAGE_CONTACT)) {
            base = NAME_PAGE_CONACT;
        }

        if (base == null) {
            return null;
        }

        String servletCxt = request.getServletContext().getContextPath();
        String query = request.getQueryString();

        base = servletCxt + "/" + base;
        if (query == null) {
            return new HtmlUri(base, null);
        } else {
            String lan = MainTabPageParams.parse(query, false).getLanguages();
            return new HtmlUri(base, lan);
        }
    }

    public String getHtmlPageUri(String servletContext, String pageName,
            String query) {
        return servletContext + "/" + pageName + "_" + query
                + UriManager.HTML_SUFFIX;
    }

    public void forwardToHtml(HttpServletRequest request,
            HttpServletResponse response, String path) throws IOException,
            ServletException {
        String servletContext = request.getServletContext().getContextPath();
        RequestDispatcher dispatcher = request.getRequestDispatcher(path
                .replace(servletContext, ""));
        Log.d(TAG, "forwardTo uri = " + path);
        dispatcher.forward(request, response);
    }

    public void forwardToHtml(HttpServletRequest request,
            HttpServletResponse response, int page, String queryRewritten)
            throws ServletException, IOException {
        RequestDispatcher dispatcher = null;
        String uri = null;
        switch (page) {
        case INDEX_PAGE_NOT_FOUND:
            response.getWriter().write("Page Not Found !");
            break;
        case INDEX_PAGE_HOME:
            uri = JSP_PATH_PAGE_HOME.replace(".jsp", "");
            break;
        case INDEX_PAGE_APPS:
            uri = JSP_PATH_PAGE_APPS.replace(".jsp", "");
            break;
        case INDEX_PAGE_CONTACT:
            uri = JSP_PATH_PAGE_CONTACT.replace(".jsp", "");
            break;
        }

        if (uri == null) {
            return;
        }
        if (queryRewritten != null && queryRewritten.length() > 0) {
            uri = uri + "_" + queryRewritten;
        }
        uri += UriManager.HTML_SUFFIX;
        uri = UriManager.TMP + request.getServletContext().getContextPath()
                + uri;
        dispatcher = request.getRequestDispatcher(uri);
        Log.d(TAG, "forwardTo uri = " + uri);
        dispatcher.forward(request, response);
    }

    public static class HtmlUri {
        public String uri = null;
        public String baseUri = null;
        public String query = null;

        // base uri = /mpchome/home
        // query = zh
        // uri = /mpchome/home_zh.htm
        public HtmlUri(String baseUri, String query) {
            this.baseUri = baseUri;
            this.query = query;
            if (query == null) {
                uri = baseUri + UriManager.HTML_SUFFIX;
            } else {
                uri = baseUri + "_" + query + UriManager.HTML_SUFFIX;
            }
        }
    }
}
